package com.log4j.demo;

import org.apache.log4j.Logger;

public class Main {
	
	private static Logger logger = Logger.getLogger(Main.class.getCanonicalName());
	
	public static void main(String args[]) {
		logger.debug("This is a debug log message");
		logger.info("This is an info log message");
		try {
			int x = divide(3, 0);
		} catch (Exception ex) {
			logger.fatal(getStackTrace(ex));
		}
	}
	
	public static int divide(int x, int y) {
		return x / y;
	}
	
	public static String getStackTrace(Throwable throwable) {
		StackTraceElement[] errArr = throwable.getStackTrace();
		StringBuilder builder = new StringBuilder();
		builder.append(throwable.toString()).append("\n");
		for (StackTraceElement s : errArr) {
			String str = "      " + s.getClassName() + " " + s.getFileName() + " at line " + s.getLineNumber() + " in method: " + s.getMethodName() + "\n";
			builder.append(str);
		}
		return builder.toString();
	}
}